﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class TrainNeedsList : System.Web.UI.Page
    {
        protected Pagination Model;
        protected  EmpBasicInfo ebi;
        protected void Page_Load(object sender, EventArgs e)
        {
            ebi = Session["ebi"] as EmpBasicInfo;
            Model = new Pagination();
            Model.PageNumShown = 6;
            Model.CurrentPage = 1;
            Model.NumPerPage = 10;
            if (!IsPostBack)
            {
                if (Request.QueryString["pageNum"] != null)
                {
                    Model.CurrentPage = int.Parse(Request.QueryString["pageNum"]);
                }
                if (Request.QueryString["numPerPage"] != null)
                {
                    Model.NumPerPage = int.Parse(Request.QueryString["numPerPage"]);
                }
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                var v = ctx.EmpDepPositions.Where(r => r.EmpId == ebi.Id && r.PosId <= 3);
                var v2 = from r in ctx.EmpRoles where r.EmpId == ebi.Id join r2 in ctx.RoleControllerDepts on r.RoleId equals r2.RoleId where r2.DeptId == null && (r2.Controller == "TrainNeeds" || r2.Controller == null) select 0;
                string strSql;
                if (v.Count() > 0 || v2.Count() > 0 || ebi.EmpRoles.Where(r => r.RoleId == 1).Any())
                {
                    strSql = @"with t as (select Id,Pid,cast(Text as nvarchar(500)) Text,OrderNo,1 lvl,cast(Id as nvarchar(50)) orderStr from Depts where Pid is null
union all
select d.Id,d.Pid,cast(REPLICATE('--',t.lvl)+d.Text as nvarchar(500)),
d.OrderNo,t.lvl+1,cast(t.orderStr+cast(d.Id as nvarchar(50)) as nvarchar(50))
from Depts d,t where d.Pid=t.id and d.IsValid=1
)
  select Id,Text from t order by orderStr;";
                }
                else
                {
                    strSql = string.Format(@"with t as(
SELECT b.Id,b.Text FROM EmpDepPositions a,Depts b where a.DeptId=b.Id and a.PosId<=7 and a.EmpId={0}
union all
select isnull(c.Id,1),isnull(c.Text,'太阳神直销总部') from EmpRoles a
	inner join RoleControllerDepts b on a.RoleId=b.RoleId and a.EmpId={0} and (b.Controller='{1}' or b.Controller is null)
	left join Depts c on b.DeptId=c.Id
union all
select a.Id,a.Text from Depts a,t where a.Pid=t.Id and a.IsValid=1
)
select distinct Id,Text from t;", ebi.Id, "TrainNeeds");
                }
                var depts = ctx.ExecuteQuery<DeptDTO>(strSql);
                Dept.DataSource = depts;
                Dept.DataValueField = "Id";
                Dept.DataTextField = "Text";
                Dept.DataBind();

                if (Request.QueryString["deptId"] != null)
                {
                    Dept.Value = Request.QueryString["deptId"];
                }
                _getDatas();
            }
        }

        protected void btnSeach_Click(object sender, EventArgs e)
        {
            _getDatas();
        }

        private void _getDatas()
        {
            int sNum = (Model.CurrentPage - 1) * Model.NumPerPage + 1;
            int eNum = Model.CurrentPage * Model.NumPerPage;
            string strSql = string.Format(@"with t as(select Id,Text from Depts where Id={0}
union all
select a.Id,a.Text from Depts a,t where a.Pid=t.Id),
t2 as(SELECT ROW_NUMBER() over(order by FormNo desc) rowNum,
FormNo,DeptName,a.EmpId,c.Name,isnull(UpdateDate,CreatDate) LastUpdateDate,StatusId
  FROM TrainNeeds a,EmpDepPositions b,t,Employee c
  where a.EmpId=b.EmpId and b.DeptId=t.Id and a.DeptName=t.Text and a.EmpId=c.Id and (a.StatusId!=0 or a.EmpId={1}))
  select FormNo,DeptName,EmpId,Name,LastUpdateDate,StatusId from t2 where rowNum between {2} and {3};", Dept.Value, ebi.Id, sNum, eNum);
            string strSql2 = string.Format(@"with t as(select Id,Text from Depts where Id={0}
union all
select a.Id,a.Text from Depts a,t where a.Pid=t.Id)
SELECT COUNT(0) cnt
  FROM TrainNeeds a,EmpDepPositions b,t
  where a.EmpId=b.EmpId and b.DeptId=t.Id and a.DeptName=t.Text and (a.StatusId!=0 or a.EmpId={1});", Dept.Value, ebi.Id);
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            var list = ctx.ExecuteQuery<TrainNeedDTO>(strSql);
            rptList.DataSource = list;
            rptList.DataBind();

            var list2 = ctx.ExecuteQuery<int>(strSql2);
            Model.TotalCount = list2.First();
        }
    }
}